var locale = localStorage.lang == 'en' ? 'en' : 'zh_CN';

(function () {
	function f(s) {
		document.write('<script src="/scriptZIP/' + s + '.js"></script>');
		

	}

	f('vee-validate.min');
	f(locale);
})();


function merge(a) {
	var o = {
		el: '#vue-root',
		components: {},
		data: {
			item: {},
			url: null
		},
		created: function () {
			this.init();
		},
		methods: {
			init: function () {
				var id = params().id;
				if (!id) return;

				axios.get(this.url + '/' + id).then(function (res) {
					var data = res.data;
					this.formatData(data);
					this.item = data;
					this.copy = copy(data);
				}.bind(this));
			},
			formatData: function (data) {
			},
			fillData: function () {
			},
			submit: function () {
				var success = function () {
					alert(locale == 'en' ? 'succeed' : '成功');
					history.back();
				};
				this.$validator.validate().then(function (result) {
					if (result) {
						this.fillData();
						if (this.item.id) {
							var o = compare(this.item, this.copy);
							axios.put(this.url, o).then(success);
						} else {
							axios.post(this.url, this.item).then(success);
						}
					} else {
						alert(locale == 'en' ? 'Please enter the complete information' : '请输入完整信息！');
					}
				}.bind(this));
			},
			err: function (field) {
				return this.errors.has(field) ? '*' + this.errors.first(field) : null;
			}
		},
		computed: {},
		watch: {}
	};

	['el', 'created', 'i18n'].forEach(function (it) {
		if (it in a) o[it] = a[it];
	});
	['data', 'methods', 'computed', 'watch', 'components'].forEach(function (it) {
		if (it in a) for (var i in a[it]) o[it][i] = a[it][i];
	});

	while (typeof VeeValidate == 'undefined') console.clear();

	VeeValidate.Validator.localize(locale);
	Vue.use(VeeValidate, { locale: locale });

	VeeValidate.Validator.extend('phone', {
		getMessage: function (field) {
			return field + (locale == 'en' ? ' must be a valid mobile phone number' : '必须是有效的手机号码');
		},
		validate: function (value) {
			return /^1[3-9]\d{9}|6\d{7}$/.test(value);
		}
	});

	return o;
}